package cn.exrick.xboot.modules.gatestation.serviceimpl;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.gatestation.dao.NaturalgasEquipmentDao;
import cn.exrick.xboot.modules.gatestation.entity.NaturalgasEquipment;
import cn.exrick.xboot.modules.gatestation.service.NaturalgasEquipmentService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * 天燃气门站设备接口实现
 * @author yck
 */
@Slf4j
@Service
@Transactional
public class NaturalgasEquipmentServiceImpl implements NaturalgasEquipmentService {

    @Autowired
    private NaturalgasEquipmentDao naturalgasEquipmentDao;

    @Override
    public NaturalgasEquipmentDao getRepository() {
        return naturalgasEquipmentDao;
    }

    @Override
    public Page<NaturalgasEquipment> findByCondition(NaturalgasEquipment naturalgasEquipment, SearchVo searchVo, Pageable pageable) {

        return naturalgasEquipmentDao.findAll(new Specification<NaturalgasEquipment>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<NaturalgasEquipment> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                Path<String> gateStationIdField = root.get("gateStationId");
                List<Predicate> list = new ArrayList<Predicate>();
               if(StrUtil.isNotBlank(naturalgasEquipment.getGateStationId())){
                   list.add(cb.equal(gateStationIdField,naturalgasEquipment.getGateStationId()));
               }
                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public List<NaturalgasEquipment> findByEquipmentNameAndAndGateStationId(String equipmentName, String gateStationId) {
        return naturalgasEquipmentDao.findByEquipmentNameAndAndGateStationId(equipmentName,gateStationId);
    }

    @Override
    public List<NaturalgasEquipment> findByEquipmentIdAndGateStationId(String equipmentId, String gateStationId) {
        return naturalgasEquipmentDao.findByEquipmentIdAndGateStationId(equipmentId,gateStationId);
    }

    @Override
    public List<NaturalgasEquipment> findByContidion(String equipmentId, String gateStationId, String equipmentName,String id) {
        return naturalgasEquipmentDao.findByContidion(equipmentId,gateStationId,equipmentName,id);
    }

    @Override
    public List<NaturalgasEquipment> findByGateStationId(String gateStationId) {
        return naturalgasEquipmentDao.findByGateStationId(gateStationId);
    }
}